﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Liang.Util
{
    public class PDF417Code
    {
        //图像压缩Dll
        [DllImport("WComp.dll", CharSet = CharSet.Auto)]
        public static extern int SizeDIB(IntPtr szInputFile, int height, int width);//设置大小
        [DllImport("WComp.dll", CharSet = CharSet.Auto)]
        public static extern int Wcompress(IntPtr szInputFile, IntPtr szOutputFile, int nDestineSize);	//图像压缩
        [DllImport("WComp.dll", CharSet = CharSet.Auto)]
        public static extern int Wdecompress(IntPtr szInputFile, IntPtr szOutputFile);//图像解压
        //PDF417接口
        [DllImport("EnCodePdf.dll", CharSet = CharSet.Auto)]
        public static extern int InitRead(int hcallwnd, IntPtr IpPathName);	//初始化端口
        [DllImport("EnCodePdf.dll", CharSet = CharSet.Auto)]
        public static extern int CloseRead();    //关闭端口   
        [DllImport("EnCodePdf.dll", CharSet = CharSet.Auto)]
        public static extern void SetPdfConFile(IntPtr strConFileName);   //设置配置参数文件
        [DllImport("EnCodePdf.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.BStr)]

        public static extern string EnPdfText(IntPtr strInfo, IntPtr barFile);  //将信息生成PDF417条码
        [DllImport("EnCodePdf.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.BStr)]


        public static extern string MakePdf417(IntPtr txtFile, IntPtr imgFile, IntPtr othFile, IntPtr barFile); //将文本、图形和其他信息合成生成PDF417


        public static Image CreatePDF417Code(string content)
        {
            Image resultImage = null;
            try
            {             
                //取配置文件信息
                string strConFile = System.Windows.Forms.Application.StartupPath;
                strConFile += @"\MakeBarCode.ini";

                string outfile = System.Windows.Forms.Application.StartupPath;
                outfile += @"\TempBar.bmp";
                string strTxtFile = System.Windows.Forms.Application.StartupPath;
                strTxtFile += @"\Temp.txt";

                //制码
                bool bRet = MakeTextBarCode(content, strConFile, outfile);
                //显示位图
                if (bRet)
                {
                    resultImage = Image.FromFile(outfile);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PDF417码生成失败:"+ex.Message);
            }
            return resultImage;
        }

        public static bool MakeTextBarCode(string strInfo, string inifile, string outfile)
        {
            IntPtr szInfo = (IntPtr)Marshal.StringToHGlobalAnsi(strInfo);
            IntPtr szIniPath = (IntPtr)Marshal.StringToHGlobalAnsi(inifile);
            IntPtr szOutPath = (IntPtr)Marshal.StringToHGlobalAnsi(outfile);
            //生成条码
            string bmpfile = "";
            //PDF
            SetPdfConFile(szIniPath);
            bmpfile = EnPdfText(szInfo, szOutPath);

            if (bmpfile.Length < 1) return false;
            return true;
        }
    }
}
